package com.flyduck.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.common.util.IdUtil;
import com.flyduck.common.util.RSAUtil;
import com.flyduck.common.util.StringUtil;
import com.flyduck.common.util.TokenUtil;
import com.flyduck.common.vo.PageResult;
import com.flyduck.system.dao.*;
import com.flyduck.system.enums.UserTypeEnum;
import com.flyduck.system.vo.user.*;
import com.flyduck.system.vo.user.trans.SysUserAddReqVOToSysUserEntity;
import com.flyduck.system.vo.user.trans.SysUserEntityToSysUserDetailRespVO;
import com.flyduck.system.vo.user.trans.SysUserEntityToSysUserRespVO;
import com.flyduck.system.vo.user.trans.SysUserUpdateReqVOToSysUserEntity;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.flyduck.system.entity.SysUserEntity;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysUserService {


    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Autowired
    private SysUserPostDao sysUserPostDao;
    @Autowired
    private SysConfigDao sysConfigDao;


    public PageResult<SysUserRespVO> getUserPageList(SysUserReqVO reqVo, Integer page, Integer size) {
        //************************* 1.构建分页条件 *************************
        Page<SysUserEntity> userEntityPage = new Page<>(page, size);
        //************************* 2.构建查询条件 *************************
        QueryWrapper<SysUserEntity> wrapper = getUserQueryWrapper(reqVo);
        //************************* 3.查询 *************************
        IPage<SysUserEntity> userEntityIPage = sysUserDao.page(userEntityPage, wrapper);
        //************************* 4.结果返回转换 *************************
        List<SysUserRespVO> respVOList = Lists.transform(userEntityIPage.getRecords(), new SysUserEntityToSysUserRespVO(sysDeptDao));
        return PageResult.ofPages(respVOList, userEntityIPage);
    }

    private QueryWrapper<SysUserEntity> getUserQueryWrapper(SysUserReqVO reqVo) {
        QueryWrapper<SysUserEntity> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(reqVo.getNickName())) {
            wrapper.like("nick_name", reqVo.getNickName());
        }
        wrapper.eq("user_type", UserTypeEnum.COMMON_USER.getValue());
        return wrapper;
    }

    /**
     * 获取用户详细
     *
     * @param userId
     * @return
     */
    public SysUserDetailRespVO getUserDetail(Long userId) {
        SysUserEntity sysUserEntity = sysUserDao.getById(userId);
        SysUserDetailRespVO respVO = new SysUserEntityToSysUserDetailRespVO().apply(sysUserEntity);
        //获取用户角色关系集合
        List<Long> roleIds = sysUserRoleDao.getRoleIdsByUserId(userId);
        //获取用户职位关系集合
        List<Long> postIds = sysUserPostDao.getPostIdsByUserId(userId);
        respVO.setRoleIds(roleIds.stream().map(x -> x + "").collect(Collectors.toList()));
        respVO.setPostIds(postIds.stream().map(x -> x + "").collect(Collectors.toList()));
        return respVO;
    }


    /**
     * 新增用户
     *
     * @param reqVo
     * @return
     */
    @Transactional
    public boolean addUser(SysUserAddReqVO reqVo) {
        SysUserEntity sysUserEntity = new SysUserAddReqVOToSysUserEntity().apply(reqVo);
        //1.新增用户
        sysUserEntity.setId(IdUtil.getId());
        sysUserEntity.setUserType(UserTypeEnum.COMMON_USER.getValue());
        String initPassword = sysConfigDao.findConfigValueByConfigKey("sys.user.initPassword");
        sysUserEntity.setPassword(RSAUtil.encrypt(initPassword));
        sysUserEntity.setAvatar(reqVo.getAvatar());
        sysUserEntity.setCreateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        sysUserEntity.setUpdateTime(new Date());
        sysUserDao.save(sysUserEntity);
        //2.新增用户角色关系集合
        sysUserRoleDao.insertUserRoleList(sysUserEntity.getId(), reqVo.getRoleIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList()));
        //3.新增用户职位关系集合
        sysUserPostDao.insertUserPostList(sysUserEntity.getId(), reqVo.getPostIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList()));
        return true;
    }

    /**
     * 修改用户
     *
     * @param reqVo
     * @return
     */
    @Transactional
    public boolean updateUser(SysUserUpdateReqVO reqVo) {
        SysUserEntity sysUserEntity = new SysUserUpdateReqVOToSysUserEntity().apply(reqVo);
        //1.删除用户角色关系集合
        sysUserRoleDao.deleteByUserId(sysUserEntity.getId());
        //2.删除用户角色关系集合
        sysUserPostDao.deleteByUserId(sysUserEntity.getId());
        //3.修改用户
        sysUserEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        sysUserEntity.setUpdateTime(new Date());
        sysUserDao.updateById(sysUserEntity);
        //4.新增用户角色关系集合
        sysUserRoleDao.insertUserRoleList(sysUserEntity.getId(), reqVo.getRoleIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList()));
        //5.新增用户职位关系集合
        sysUserPostDao.insertUserPostList(sysUserEntity.getId(), reqVo.getPostIds().stream().map(x -> Long.valueOf(x)).collect(Collectors.toList()));
        return true;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @Transactional
    public boolean deleteUser(Long userId) {
        //1.删除用户角色关系集合
        sysUserRoleDao.deleteByUserId(userId);
        //2.删除用户角色关系集合
        sysUserPostDao.deleteByUserId(userId);
        //3.删除用户
        sysUserDao.removeById(userId);
        return true;
    }


}


